from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Cate,Group
# Create your views here.
# 获取所有分类信息
class Groups(APIView):
    def get(self,request):
        page=int(request.query_params.get('page'))
        pagesize=int(request.query_params.get('pagesize'))
        group=Group.objects.all()[(page-1)*pagesize:(page*pagesize)]
        # group=Group.objects.get(id=cate.id)
        group_list=[]
        for i in group:
            # 根据id 获取种类信息
            cname=Cate.objects.get(id=i.cate_id)
            # print(cname)
            group_list.append({
                'id':i.id,
                'cate_name':cname.name,
                'cate':i.cate_id,
                'name':i.name,
                # 'cid':i.name,
                'sequence':i.sequence,
                'group_url':i.group_url,
            })
        return Response({
            'code':200,
            'msg':'获取全部成功',
            'result':group_list,
            'total':Group.objects.all().count()
        })
# 获取种类信息
class CateAll(APIView):
    def get(self,request):
        cate = Cate.objects.all()
        cate_list=[]
        for i in cate:
            cate_list.append({
                'id':i.id,
                'name':i.name
            })
        return Response(cate_list)
# 获取分类信息
class GroupAll(APIView):
    def get(self,request):
        group = Group.objects.all()
        group_list = []
        for i in group:
            group_list.append({
                'id': i.id,
                'name': i.name,
            })
        return Response(group_list)
# 添加分类
class Add(APIView):
    def post(self,request):
        # 获取前端参数
        name=request.data.get('group_name')
        sequence=request.data.get('sequence')
        group_url=request.data.get('url')
        cate=request.data.get('category_name')
        print(name,sequence,group_url,cate)
        # 判断参数是否齐全
        if not all([name,sequence,group_url,cate]):
            return Response({'code':400,'msg':'参数不齐全'})
        # 判断类别名是否存在
        try:
            Cate.objects.get(name=cate)
        except Exception as e:
            print(e)
            try:
                cate=Cate.objects.create(name=cate)
            except Exception as e:
                print(e)
                return Response({'code': 400, 'msg': '一级类别添加失败'})
        try:
            Group.objects.create(name=name,sequence=sequence,group_url=group_url,cate_id=cate.id)
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'添加失败'})
        return Response({'code':201,'msg':'添加成功'})
class GroupInfo(APIView):
    def get(self,request,id):
        try:
            group=Group.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '类别获取失败'})
        cate=Cate.objects.get(id=group.cate_id)
        return Response({
            'name':group.name,
            'id':group.id,
            'group_url':group.group_url,
            'sequence':group.sequence,
            'cate_name':cate.name,
            'cate':cate.id
        })
    def put(self,request,id):
        name = request.data.get('name')
        sequence = request.data.get('sequence')
        group_url = request.data.get('group_url')
        cate_name = request.data.get('cate')
        print(name,sequence,group_url,cate_name)
        cate=Cate.objects.get(name=cate_name)
        print(type(cate))
        # data = Group.objects.get(name=name)

        try:
            group=Group.objects.get(id=id)
            group.name=name
            # group.id=id
            group.cate_id=cate.id
            group.group_url=group_url
            group.sequence=sequence
            group.save()
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '类别修改失败'})
        return Response({'code': 200, 'msg': '类别修改成功'})
    def delete(self,request,id):
        try:
            group=Group.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '类别删除失败'})
        return Response({'code': 200, 'msg': '类别删除成功'})







